<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>修改</title>
</head>
<body>
<div>
    <input type="hidden" id="id">
    <!--    叫隐藏域：用户看不到，只是用来存储数据-->

    <div>
        <label>
            课程名字：<input type="text" id="name">
        </label>
    </div>
    <div>
        <label>
            课程编号：<input type="text" id="no">
        </label>
    </div>
    <div>
        <button onclick="changeClick()">修改</button>
    </div>
</div>
<script src="ajax.js"></script>
<script src="common.js"></script>
<script>
    window.onload = function () {
        let formData = new FormData();
        //调用common里 GetQueryString方法 获取ID
        let id = GetQueryString("id");
        formData.append("id", id);
        //定义回调函数
        function updateInput(data) {
            if (data.code == "200") {
                //获取Data里的参数
                let course = data.data;
                //获取每行文本的值
                document.querySelector("#name").value =course.courseName;
                document.querySelector("#no").value = course.courseNo;
            }else{
                //调用后台错误信息
                alert(data.msg);
            }
        }
        //调用ajax get.php，传参 定义好的formData和定义好的回调函数；
        ajax("course/get.php", "POST", formData, updateInput);

    }
    let id = getQueryStringByName("id");
        if (!id) {
        location.href = "/courseAdd.html"
    }
    document.querySelector("#id").value = id;

    function changeClick() {
        change();
    }

    function changeRes(jsonObj) {
        alert("修改成功");
        window.location.href = "/courseList.html"
    }

    function change() {
        let nameId = document.getElementById("name").value;
        let numId = document.getElementById("no").value;
        let formData = new FormData();//表单数据对象，即键值对
        formData.append("id", id);
        formData.append("courseName",nameId );
        formData.append("courseNo", numId);
        ajax("course/update.php", "POST", formData, changeRes);
    }
</script>
</body>
</html>